package com.ruoyi.biz.category.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.category.mapper.BizCategoryMapper;
import com.ruoyi.common.core.domain.entity.BizCategory;
import com.ruoyi.biz.category.service.IBizCategoryService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 分类Service业务层处理
 *
 * @author Fei.Cong
 * @date 2024-07-11
 */
@Service
public class BizCategoryServiceImpl extends ServiceImpl<BizCategoryMapper, BizCategory> implements IBizCategoryService {

    @Autowired
    private BizCategoryMapper bizCategoryMapper;

    /**
     * 分页查询分类
     *
     * @param page
     * @param bizCategory 分类
     */
    @Override
    public IPage<BizCategory> pageList(BizCategory bizCategory, IPage<BizCategory> page) {
        return bizCategoryMapper.selectPageList(page, bizCategory);
    }

    /**
     * 条件查询分类
     *
     * @param bizCategory 分类
     * @return
     */
    @Override
    public List<BizCategory> queryList(BizCategory bizCategory) {
        LambdaQueryWrapper<BizCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return bizCategoryMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<BizCategory> menus) {
        List<BizCategory> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param menus 菜单列表
     * @return 树结构列表
     */
    @Override
    public List<BizCategory> buildMenuTree(List<BizCategory> menus) {
        List<BizCategory> returnList = new ArrayList<BizCategory>();
        List<Long> tempList = menus.stream().map(BizCategory::getId).collect(Collectors.toList());
        for (Iterator<BizCategory> iterator = menus.iterator(); iterator.hasNext(); ) {
            BizCategory menu = (BizCategory) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId())) {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list 分类表
     * @param t    子节点
     */
    private void recursionFn(List<BizCategory> list, BizCategory t) {
        // 得到子节点列表
        List<BizCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BizCategory tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BizCategory> getChildList(List<BizCategory> list, BizCategory t) {
        List<BizCategory> tlist = new ArrayList<BizCategory>();
        Iterator<BizCategory> it = list.iterator();
        while (it.hasNext()) {
            BizCategory n = (BizCategory) it.next();
            if (n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BizCategory> list, BizCategory t) {
        return getChildList(list, t).size() > 0;
    }
}
